package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DTO.SysArchiveDTO;
import com.ruoyi.system.domain.DTO.SysArchiveFilesDTO;
import com.ruoyi.system.domain.DTO.SysArchivePageDTO;
import com.ruoyi.system.domain.SysArchive;
import com.ruoyi.system.domain.SysArchiveFiles;
import com.ruoyi.system.domain.vo.SysArchiveDetailVo;
import com.ruoyi.system.mapper.SysArchiveMapper;
import com.ruoyi.system.service.SysArchiveFilesService;
import com.ruoyi.system.service.SysArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SysArchiveImpl extends ServiceImpl<SysArchiveMapper, SysArchive> implements SysArchiveService {

    @Autowired
    private SysArchiveFilesService sysArchiveFilesService;

    @Override
    public TableDataInfo<SysArchive> pageList(SysArchivePageDTO pageDTO) {
        Page<SysArchive> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        LambdaQueryWrapper<SysArchive> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(pageDTO.getWorkName())){
            wrapper.like(SysArchive::getWorkName,pageDTO.getWorkName());
        }
        if (StringUtils.isNotEmpty(pageDTO.getDegree())){
            wrapper.eq(SysArchive::getDegree,pageDTO.getDegree());
        }
        if (Objects.nonNull(pageDTO.getMinAge())){
            wrapper.ge(SysArchive::getAge,pageDTO.getMinAge());
        }
        if (Objects.nonNull(pageDTO.getMaxAge())){
            wrapper.le(SysArchive::getAge,pageDTO.getMaxAge());
        }
        Page<SysArchive> sysArchivePage = this.page(page, wrapper);
        return TableDataInfo.<SysArchive>builder()
                .msg("")
                .code(HttpStatus.SUCCESS)
                .total(sysArchivePage.getTotal())
                .rows(sysArchivePage.getRecords())
                .build();
    }

    @Override
    public void addArchive(SysArchiveDTO sysArchiveDTO) {
        if (Objects.isNull(sysArchiveDTO)){
            throw new ServiceException("请求参数不能为空");
        }
        SysArchive sysArchive = BeanUtil.copyProperties(sysArchiveDTO, SysArchive.class);
        this.save(sysArchive);
        if (!sysArchiveDTO.getFilesList().isEmpty()){
            List<SysArchiveFilesDTO> filesList = sysArchiveDTO.getFilesList();
            List<SysArchiveFiles> files = filesList.stream().map(item -> {
                SysArchiveFiles sysArchiveFiles = BeanUtil.copyProperties(item, SysArchiveFiles.class);
                sysArchiveFiles.setArchiveId(sysArchive.getId());
                sysArchiveFiles.setArchiveType(sysArchiveDTO.getArchiveType());
                return sysArchiveFiles;
            }).collect(Collectors.toList());
            sysArchiveFilesService.saveBatch(files);
        }
    }

    @Override
    public SysArchiveDetailVo detail(Long archiveId) {
        if (Objects.isNull(archiveId)){
            throw new ServiceException("档案id不能为空");
        }
        SysArchive sysArchive = this.getById(archiveId);
        if (Objects.isNull(sysArchive)){
            throw new ServiceException("不存在的档案信息");
        }
        SysArchiveDetailVo sysArchiveDetailVo = BeanUtil.copyProperties(sysArchive, SysArchiveDetailVo.class);
        LambdaQueryWrapper<SysArchiveFiles> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysArchiveFiles::getArchiveId,archiveId);
        List<SysArchiveFiles> filesList = sysArchiveFilesService.list(wrapper);
        sysArchiveDetailVo.setFilesList(filesList);
        return sysArchiveDetailVo;
    }
}
